package com.jet.goods.controller;

import com.jet.core.model.vo.PageVo;
import com.jet.core.result.ResultJson;
import com.jet.goods.domain.dto.SkuSingleDto;
import com.jet.goods.domain.query.SkuPageQuery;
import com.jet.goods.domain.vo.SkuDetailListVo;
import com.jet.goods.domain.vo.SkuDetailVo;
import com.jet.goods.service.IGoodsSkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author dcl
 * @since 2024/11/28 21:08
 */
@RestController
@RequestMapping("/goods/sku")
@Slf4j
public class SkuController {

    private final IGoodsSkuService goodsSkuService;

    public SkuController(IGoodsSkuService goodsSkuService) {
        this.goodsSkuService = goodsSkuService;
    }

    @PostMapping
    public ResultJson<Void> create(@RequestHeader long merchantNo,
                                   @RequestHeader long storeNo,
                                   @RequestHeader String accountId,
                                   @Validated @RequestBody SkuSingleDto dto) {
        goodsSkuService.save(merchantNo, accountId, dto);
        return ResultJson.ok();
    }

    @PutMapping("/{skuId}")
    public ResultJson<Void> edit(@RequestHeader long merchantNo,
                                 @RequestHeader long storeNo,
                                 @RequestHeader String accountId,
                                 @PathVariable("skuId") String skuId,
                                 @RequestBody SkuSingleDto dto) {
        goodsSkuService.edit(merchantNo, accountId, skuId, dto);
        return ResultJson.ok();
    }

    @DeleteMapping("/{skuId}")
    public ResultJson<Void> delete(@RequestHeader long merchantNo,
                                   @RequestHeader long storeNo,
                                   @RequestHeader String accountId,
                                   @PathVariable("skuId") String skuId) {
        goodsSkuService.deleteBySkuId(merchantNo, skuId);
        return ResultJson.ok();
    }

    @GetMapping("/{skuId}")
    public ResultJson<SkuDetailVo> info(@RequestHeader long merchantNo,
                                        @RequestHeader long storeNo,
                                        @RequestHeader String accountId,
                                        @PathVariable("skuId") String skuId) {
        return ResultJson.ok(goodsSkuService.findDetailById(merchantNo, skuId));
    }

    @GetMapping("/page")
    public ResultJson<PageVo<SkuDetailListVo>> page(@RequestHeader long merchantNo,
                                                    @RequestHeader long storeNo,
                                                    @RequestHeader String accountId,
                                                    SkuPageQuery query) {
        return ResultJson.ok(goodsSkuService.page(merchantNo, storeNo, accountId, query));
    }
}
